<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <el-alert title="注意：只允许为第三级分类设置相关参数！" type="warning" show-icon :closable="false"> </el-alert>
      <el-row class="cat_opt">
        <el-col>
          <span
            >选择商品分类：
            <el-cascader v-model="selectCateKeys" :options="parentCateList" :props="treeProps" @change="handleChange" clearable> </el-cascader>
          </span>
        </el-col>
      </el-row>

      <el-row>
        <el-col>
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="动态参数" name="many">
              <el-button type="primary" size="mini" :disabled="this.selectCateKeys.length != 3">添加参数</el-button>

              <el-table :data="manyTableData" border stripe>
                <el-table-column type="expand">
                  <template slot-scope="scope">
                    <el-tag v-for="(item, index) in scope.row.attrVals" :key="index" closable @close="removeAtreById(scope.row, index)">{{ item }}</el-tag>

                    <el-input
                      class="input-new-tag"
                      style="width: 120px"
                      v-if="scope.row.inputVisible"
                      v-model="scope.row.inputValue"
                      ref="saveTagInput"
                      size="small"
                      @keyup.enter.native="handleInputConfirm(scope.row)"
                      @blur="handleInputConfirm(scope.row)"
                    >
                    </el-input>
                    <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
                  </template>
                </el-table-column>
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column prop="attrName" label="参数名"></el-table-column>
                <el-table-column label="操作" width="180px">
                  <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" size="mini" @click="editDiglogInit(scope.row)"></el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteUserById(scope.row.id)"></el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>

            <el-tab-pane label="静态属性" name="only">
              <el-button type="primary" size="mini" :disabled="isBtnDisabled">添加属性</el-button>

              <el-table :data="onlyTableData" border stripe>
                <el-table-column type="expand">
                  <template slot-scope="scope">
                    <el-tag v-for="(item, index) in scope.row.attrVals" :key="index" closable @close="removeAtreById(scope.row, index)">{{ item }}</el-tag>

                    <el-input
                      class="input-new-tag"
                      style="width: 120px"
                      v-if="scope.row.inputVisible"
                      v-model="scope.row.inputValue"
                      ref="saveTagInput"
                      size="small"
                      @keyup.enter.native="handleInputConfirm(scope.row)"
                      @blur="handleInputConfirm(scope.row)"
                    >
                    </el-input>
                    <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
                  </template>
                </el-table-column>
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column prop="attrName" label="属性名"></el-table-column>
                <el-table-column label="操作" width="180px">
                  <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" size="mini" @click="editDiglogInit(scope.row)"></el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteUserById(scope.row.id)"></el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane> </el-tabs
        ></el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectCateKeys: [],
      parentCateList: [],
      treeProps: {
        expandTrigger: 'hover',
        checkStrictly: false,
        value: 'id',
        label: 'catName',
        children: 'children'
      },
      activeName: 'many',

      manyTableData: [],
      onlyTableData: []
    }
  },

  created() {
    this.getParentCateList()
  },

  computed: {
    isBtnDisabled() {
      return this.selectCateKeys.length != 3
    },

    cateId() {
      if (this.selectCateKeys.length === 3) {
        return this.selectCateKeys[this.selectCateKeys.length - 1]
      }
      return null
    }
  },

  methods: {
    handleChange() {
      this.getParamsData()
    },

    handleClick() {
      this.getParamsData()
    },

    // 获取父级分类的数据列表
    async getParentCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 3 }
      })

      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类数据失败！')
      }

      this.parentCateList = res.data.result
    },

    async getParamsData() {
      if (this.selectCateKeys.length != 3) {
        this.selectCateKeys = []
        this.manyTableData = []
        this.onlyTableData = []
        return
      }
      const { data: resp } = await this.$http.get(`categories/${this.cateId}/attributes`, {
        params: { sel: this.activeName }
      })
      if (resp.meta.status !== 200) {
        return this.$message.error('获取分类参数列表失败')
      }
      resp.data.forEach(item => {
        item.attrVals = item.attrVals ? item.attrVals.split(' ') : []
        item.inputVisible = false
        item.inputValue = ''
      })
      if (this.activeName === 'many') {
        this.manyTableData = resp.data
      } else {
        this.onlyTableData = resp.data
      }
    },

    editDiglogInit(cate) {},
    deleteUserById(cateId) {},

    async handleInputConfirm(row) {
      if (row.inputValue.trim().length === 0) {
        row.inputValue = ''
        row.inputVisible = false
        return
      }
      row.attrVals.push(row.inputValue.trim())
      row.inputValue = ''
      row.inputVisible = false
      this.saveAttrVals(row)
    },

    async saveAttrVals(row) {
      const { data: resp } = await this.$http.put(`categories/${this.cateId}/attributes/${row.id}`, {
        attrName: row.attrName,
        attrSel: row.attrSel,
        attrVals: row.attrVals.join(' ')
      })
      if (resp.meta.status !== 200) {
        return this.$message.error(resp.meta.msg)
      }
      return this.$message.success('修改参数项成功')
    },

    removeAtreById(row, index) {
      row.attrVals.splice(index, 1)
      this.saveAttrVals(row)
    },

    showInput(row) {
      row.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    }
  }
}
</script>
<style lang='less' scoped>
.cat_opt {
  margin: 15px 0;
}
.el-tag {
  margin: 7px;
}
</style>